﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
using QT.Windows.Shell;

namespace IMClient
{
    public partial class Main : GlassForm
    {
        #region Atrributes

        private static byte[] message = new byte[1024];
        private string sid = "127.0.0.1";
        private int sport = 8885;
        Socket clientSocket = null;

        #endregion

        #region Form

        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            //设定服务器IP地址  
            IPAddress ip = IPAddress.Parse(sid);
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                clientSocket.Connect(new IPEndPoint(ip, sport)); //配置服务器IP与端口  
            }
            catch
            {
                MessageBox.Show("连接服务器失败");
                Login login = new Login();
                login.Show();
                this.Close();
                return;
            }
            //通过clientSocket接收数据  
            int receiveLength = clientSocket.Receive(message);
            //MessageBox.Show(string.Format("接收服务器消息：{0}", Encoding.ASCII.GetString(message, 0, receiveLength)));
            //通过 clientSocket 发送数据  
            //for (int i = 0; i < 10; i++)
            //{
            //    try
            //    {
            //        string input = i.ToString();
            //        clientSocket.Send(Encoding.ASCII.GetBytes(input));
            //    }
            //    catch
            //    {
            //        clientSocket.Shutdown(SocketShutdown.Both);
            //        clientSocket.Close();
            //        break;
            //    }
            //}   
        }

        private void Main_LocationChanged(object sender, EventArgs e)
        {
            if (this.Right >= Screen.PrimaryScreen.Bounds.Width)
            {
                if (this.Right < Screen.PrimaryScreen.Bounds.Width + this.Width)
                {
                    this.Location = new Point(this.Location.X + 1, this.Location.Y);
                }
            }
            if (this.Top <= 0)
            {
                if (this.Top > 1 - this.Height)
                {
                    this.Top--;
                }
            }
        }

        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        #endregion

        Option myOption = null;
        private void pbSetting_Click(object sender, EventArgs e)
        {
            if (myOption != null)
            {
                myOption.BringToFront();
                return;
            }
            myOption=new Option();
            myOption.Show();
        }
    }
}
